﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using Model;
using Model.Request;
using Model.Response;
using Services;
using Services.Question;
using TCScenery.BirdNest.Common.QuickExtend;

namespace UI.QuestionSet
{
    /// <summary>
    /// AjaxCall 的摘要说明
    /// </summary>
    public class AjaxCall : IHttpHandler
    {


        public void ProcessRequest(HttpContext context)
        {
            var act = context.Request.QueryString["action"];
            var serid = context.Request.QueryString["Serid"];
            var jsonString = context.Request.InputStream.ReadAsString();
            if (act == "SetQuestion")
            {
                SetQuestion(context, jsonString);
            }
            if (act == "GetQuestionPaperInfo")
            {
                GetQuestionPaperInfo(context, serid);
            }
            if (act == "SaveAnswerRecords")
            {
                SaveAnswerRecords(context, jsonString);
            }

            if (act == "GetPageList")
            {
                GetPageList(context);
            }

            //    context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        ///获取当前登陆人的问卷



        public void GetPageList(HttpContext context)
        {
            var userId = context.Request.QueryString["userId"];
            var res = QuestionServices.GetPageList(Convert.ToInt16( userId) );
            ResponseAjaxContent(context, JsonHelper.Serialize(res));
        }

        /// <summary>
        /// 保存问卷数据
        /// </summary>
        /// <param name="context"></param>
        /// <param name="data"></param>
        public void SetQuestion(HttpContext context,string data)
        {
          var res =   QuestionServices.SetQuestion(data);

            ResponseAjaxContent(context,JsonHelper.Serialize(res));
        }

        /// <summary>
        /// 获取答卷数据
        /// </summary>
        /// <param name="context"></param>
        /// <param name="data"></param>
        public void GetQuestionPaperInfo(HttpContext context, string serid)
        {
            var res = QuestionServices.GetQuestionPaperInfo(serid);

            ResponseAjaxContent(context, JsonHelper.Serialize(res));
        }
        /// <summary>
        /// 保存答卷
        /// </summary>
        /// <param name="context"></param>
        /// <param name="data"></param>
        public void SaveAnswerRecords(HttpContext context, string data)
        {
            var res = QuestionServices.SaveAnswerRecords(data);

            ResponseAjaxContent(context, JsonHelper.Serialize(res));
        }

        /// <summary>
        /// 输出Ajax内容
        /// </summary>
        /// <param name="context"></param>
        /// <param name="Content"></param>
        private void ResponseAjaxContent(HttpContext context,string Content)
        {
            context.Response.Clear();
            context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
            context.Response.Write(Content);
            context.Response.End();
        }
    }

    
}